package com.grj.template.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grj.template.boot.domain.Role;
import com.grj.template.boot.service.RoleService;
import com.grj.template.boot.mapper.RoleMapper;
import com.grj.template.boot.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author eren
 * @description 针对表【role】的数据库操作Service实现
 * @createDate 2024-10-21 15:45:24
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public Page<Role> fetchRolePage(int currentPage, int pageSize, Role role, List<Date> during) {
        QueryWrapper<Role> roleQueryWrapper = Wrappers.query();
        if (during != null && !during.isEmpty()) {
            roleQueryWrapper.between("create_time", during.get(0), during.get(1));
        }
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(role);
        HashMap<String, Object> map = new HashMap<>();
        stringObjectMap.forEach((k, v) -> {
            if (v != null) {
                map.put(StringUtil.toCamelCase(k), v);
            }
        });
        roleQueryWrapper.allEq(map, false);
        return baseMapper.selectPage(new Page<>(currentPage, pageSize), roleQueryWrapper);
    }

    @Override
    public void postRole(Role role) {
        baseMapper.insert(role);
    }

    @Override
    public void putRole(Role role) {
        baseMapper.updateById(role);
    }

    @Override
    public void delRole(Long roleId) {
        baseMapper.deleteById(roleId);
    }

    @Override
    public void batchDelRole(List<Long> roleIds) {
        baseMapper.deleteBatchIds(roleIds);
    }

    @Override
    public Role fetchRoleById(Long roleId) {
        return baseMapper.selectById(roleId);
    }
}




